package cn.edu.zut.controller;

import cn.edu.zut.domin.Book;
import cn.edu.zut.domin.BookLendInfo;
import cn.edu.zut.domin.Reader;
import cn.edu.zut.service.IBookLendInfoService;
import cn.edu.zut.service.IBookService;
import cn.edu.zut.service.IReaderService;
import com.github.pagehelper.PageInfo;
import javafx.util.Pair;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 读者执行借书操作
 */

@Controller
@Transactional
@RequestMapping("/bookLendByReader")
public class BookLendByReaderController {

    ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
    IBookService bookService=(IBookService) ac.getBean("bookServiceImpl");
    IReaderService iReaderService=(IReaderService) ac.getBean("readerServiceImpl");
    IBookLendInfoService bookLendInfoService =(IBookLendInfoService)ac.getBean("bookLendInfoServiceImpl");


    /**
     * 查询所有图书的借阅情况(方便借阅)
     * @param id
     * @return
     * @throws Exception
     */
    Reader reader1 = new Reader();
    @RequestMapping("/findAll")
    public ModelAndView findAll(String id) throws Exception {

        ModelAndView mv = new ModelAndView();
        Reader reader = iReaderService.findReaderById1(id);
        if(reader!=null)
            reader1 = reader;
//        System.out.println(reader);
//        System.out.println(reader1);
            List<Book> books = bookService.findAll();
        List<Pair<Book,String>> ll=new ArrayList<>();
       for(Book book:books){
           ll.add(new Pair<>(book,reader1.getReaderId()));
       }
            PageInfo<Pair<Book,String>> pageInfo = new PageInfo<>(ll);
            //总页数等....
            mv.addObject("pageInfo", pageInfo);
            mv.setViewName("BookLendByReader-list");
            return mv;
    }

    /**
     * 借阅成功,向借阅表中插入一条记录,否则提示借书失败
     * @param bookId
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveBookLendInfo")
    public ModelAndView saveBookLendInfo(String bookId) throws Exception {
        List<Book> books1 = bookService.findBookById(bookId);
        Book book = new Book();
        for(Book book1:books1){
            book = book1;
        }
        ModelAndView mv = new ModelAndView();
        BookLendInfo bookLendInfo = new BookLendInfo();
        List<BookLendInfo> bookLendInfos = bookLendInfoService.getBookLendInfoByBookIdAndReaderId
                (1,4,book.getBookId(),reader1.getReaderId());
        if(book.getLentAmount().equals(book.getAllAmount())){
//            如果需要提示，在这里加提示书已经全部借出的提示页面
            mv.setViewName("BookLendFailer");
            return mv;
        }else{
            if(bookLendInfos.isEmpty()) {
                System.out.println(reader1.getReaderId()+".....");
                book.setLentAmount(book.getLentAmount()+1);
                bookService.updateBook(book);
                bookLendInfo.setBookId(book.getBookId());
                bookLendInfo.setReaderId(reader1.getReaderId());
                bookLendInfo.setLendNumber(1);
                bookLendInfo.setLendDate(new Date());
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(bookLendInfo.getLendDate());
                calendar.add(Calendar.DATE,30);
                Date date = calendar.getTime();
                bookLendInfo.setDueReturnDate(date);
                bookLendInfo.setFineAmount(0.0f);
                bookLendInfo.setActualReturnDate(null);
                bookLendInfoService.saveBookLendInfo(bookLendInfo);
            }else{
//            如果你已经借阅过改图书则不能再次借阅
                mv.setViewName("BookLendFailer1");
                return mv;
            }
        }

        List<Book> books = bookService.findAll();
        List<Pair<Book,String>> ll=new ArrayList<>();
        for(Book bb:books){
            ll.add(new Pair<>(bb,reader1.getReaderId()));
        }
        PageInfo<Pair<Book,String>> pageInfo = new PageInfo<>(ll);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("BookLendByReader-list");
        return mv;
    }
}
